import { observable, action } from 'mobx';
import { message } from 'antd';
import http from '../common/http';
import paramInspect from '../common/paramInspect';

export class NewsStore {
  constructor(rootStore) {
    this.rootStore = rootStore
  }
  // News List
  @observable newsListParam = {
    "type": 0,
    "endTime": "",
    "local": "en",
    "pageIndex": 1,
    "pageSize": 10,
    "source": "",
    "startTime": "",
    "status": "0",
    "title": "",
    "delState": false
  };
  @observable newsList = [];
  @observable newsListTotal = 0;
  @observable newsListLoadSuccess = false;

  @action toGetNewsList = () => {
    if (this.newsListParam.status === "0") {
      this.rootStore.globalStore.toShowLoding();
      http.post('/article/fetchArticleForReview', paramInspect(this.newsListParam))
      .then((response) => {
        this.rootStore.globalStore.toHideLoading();
        if (response.success) {
          this.newsListLoadSuccess = true;
          this.newsList = response.data.dataList;
          this.newsListTotal = response.data.total;
        }
      })
    } else {
      this.rootStore.globalStore.toShowLoding();
      http.get('/article/list', paramInspect(this.newsListParam))
      .then((response) => {
        this.rootStore.globalStore.toHideLoading();
        if (response.success) {
          this.newsListLoadSuccess = true;
          this.newsList = response.data.dataList;
          this.newsListTotal = response.data.total;
        }
      })
    }
  }

  @action toChangeLocal = (local) => {
    this.newsListParam.local = local
    this.toGetNewsList();
  }

  @action toChangePageNumber = (pageNumber) => {
    this.newsListParam.pageIndex = pageNumber;
    this.toGetNewsList();
  }

  @action toChangePageSize = (pageSize) => {
    this.newsListParam.pageIndex = 1;
    this.newsListParam.pageSize = pageSize;
    this.toGetNewsList();
  }

  @action filterParamSearch = (param) => {
    this.newsListParam.title = param.title;
    this.newsListParam.startTime = param.startTime;
    this.newsListParam.endTime = param.endTime;
    this.newsListParam.pageIndex = 1;
    this.newsListParam.categoryId = ""
    this.toGetNewsList();
  }

  @action toChangeListStatus = (status, type, local) => {
    this.newsListParam.pageIndex = 1;
    this.newsListParam.pageSize = 10;
    this.newsListParam.status = status;
    this.newsListParam.type = type
    this.newsListParam.local = local
    this.toGetNewsList();
  }

  @action toChangeCategoryFilter = (item, id) => {
    this.newsListParam.categoryId = id;
    this.toGetNewsList();
  }

  @action toApproveNews = (ids) => {
    const param = {
      ids: ids
    }

    http.postString('/article/online', param)
    .then((response) => {

    })
  }

  // Category List
  @observable categoryParam = {
    "ascOrDesc": "desc",
    "delState": false,
    "desc": "",
    "id": "",
    "local": "en",
    "name": "",
    "orderByColumn": "",
    "pageIndex": 1,
    "pageSize": 10,
    "parentId": "",
    "resultLimit": "",
  }

  @observable categoryParamFifty = {
    "ascOrDesc": "desc",
    "delState": false,
    "desc": "",
    "id": "",
    "local": "en",
    "name": "",
    "orderByColumn": "",
    "pageIndex": 1,
    "pageSize": 50,
    "parentId": "",
    "resultLimit": "",
  }

  @observable categoryList = [];
  @observable categoryListFifty = [];
  @observable categoryListTotal: 0;
  @observable categroyLoadSuccess = false;
  @observable categoryFiftyLoadSuccess = false;

  @action toChangeCategoryPageNumber = (pageNumber) => {
    this.categoryParam.pageIndex = pageNumber;
    this.toGetNewsCategory();
  }

  @action toChangeCategoryPageSize = (pageSize) => {
    this.categoryParam.pageIndex = 1;
    this.categoryParam.pageSize = pageSize;
    this.toGetNewsCategory();
  }

  @action toGetNewsCategory = (local) => {
    this.categoryParam.local = local;
    this.rootStore.globalStore.toShowLoding();
    http.get('/news/category/page', paramInspect(this.categoryParam))
    .then((response) => {
      this.rootStore.globalStore.toHideLoading();
      if (response.success) {
        this.categoryList = response.data.dataList;
        this.categoryListTotal = response.data.total;
        this.categroyLoadSuccess = true;
      }
    })
  }

  @action toGetNewsCategoryFifty = (local) => {

    this.categoryParamFifty.local = local;
    http.get('/news/category/page', paramInspect(this.categoryParamFifty))
    .then((response) => {
      if (response.success) {
        this.categoryListFifty = response.data.dataList;
        this.categoryFiftyLoadSuccess = true
      }
    })
  }

  @action toDeleteCategory = (id) => {
    const param = {
      id: id
    }
    this.rootStore.globalStore.toShowLoding();
    http.post('/news/category/delete', param)
    .then((response) => {
      this.rootStore.globalStore.toHideLoading();
      if (response.success) {
        message.success('Success');
        this.toGetNewsCategory();
      }
    })
  }

  @action toBatchDeleteCategory = (ids) => {
    const param = {
      ids: ids
    }
    http.post('/news/category/batchDelete', param)
    .then((response) => {
      if (response.success) {
        this.toGetNewsCategory();
      }
    })
  }
}

// const newsStore = new NewsStore();

export default NewsStore;
